/**
 * calculator.js v1.0.0 by @businka and @kreedev
 * Copyright 2013 Koltsovs Inc.
 **/
var Test = 123;
var Calculator = {
    self: this,
    logger: Logger,
    A: 0,
    B: 0,
    Operation: "+",
    Input: function () {
        Logger.Info("Calculator.Input");

        var inputId = "Input";
        return document.getElementById(inputId);
    },
    Plus: function () {
        Logger.Info("Calculator.Plus");
        var self = Calculator;

        self.A = self.Input().value;
        self.Operation = "+";
        self.Input().value = "";
    },
    Minus: function () {
        Logger.Info("Calculator.Minus");
        var self = Calculator;

        self.A = self.Input().value;
        self.Operation = "-";
        self.Input().value = "";
    },
    Divide: function () {
        Logger.Info("Calculator.Divide");
        var self = Calculator;

        self.A = self.Input().value;
        self.Operation = "/";
        self.Input().value = "";
    },
    Multiply: function () {
        Logger.Info("Calculator.Multiply");
        var self = Calculator;

        self.A = self.Input().value;
        self.Operation = "*";
        self.Input().value = "";
    },
    Clear: function () {
        Logger.Info("Calculator.Clear");
        var self = Calculator;

        self.Input().value = 0;
        self.A = 0;
        self.B = 0;
        self.Operation = "";
    },
    RemoveLast: function () {
        Logger.Info("Calculator.RemoveLast");
        var self = Calculator;
        var input = self.Input();

        if (input.value.length > 1) {
            input.value = input.value.substring(0, input.value.length - 1);
        }
        else {
            input.value = 0;
        }
    },
    Equals: function () {
        var self = Calculator;

        Logger.Info("Calculator.Equals A = " + self.A + " Operation: " + self.Operation + " B = " + self.B);

        switch (self.Operation) {
            case "+":
                self.A = Number(self.A) + Number(self.B);
                break;
            case "-":
                self.A = Number(self.A) - Number(self.B);
                break;
            case "*":
                self.A = Number(self.A) * Number(self.B);
                break;
            case "/":
                self.A = Number(self.A) / Number(self.B);
                break;
            default:
                self.A = 0;
                break;
        }
        self.Input().value = self.A;
    },
    Point: function () {
        Logger.Info("Calculator.Point");

        alert("Point todo");
    },
    AddNumber: function (number) {
        Logger.Info("Calculator.AddNumber number: " + number);

        var self = Calculator;
        var input = self.Input();

        if (input.value === 0) {
            self.A = number;
            input.value = number;
        }
        else if (input.value.length < 15) {
            input.value += number;
            self.B += number;
        }
        else if (input.value === "") {
            input.value = number;
            self.B = input.value;
        }
    },
    Init: function () {
        Logger.Info("Calculator.Init");

        var self = this;
        var btnId = "btn";
        var element;

        for (var i = 0; i < 10; i++) {
            element = document.getElementById(btnId + i);
            element.addEventListener("click", function (j) {
                return function () {
                    self.AddNumber(j);
                };
            }(i), false);
        }

        element = document.getElementById("btnClear");
        element.addEventListener("click", self.Clear);

        element = document.getElementById("btnRemoveLast");
        element.addEventListener("click", self.RemoveLast);

        element = document.getElementById("btnEquals");
        element.addEventListener("click", self.Equals);

        element = document.getElementById("btn+");
        element.addEventListener("click", self.Plus);

        element = document.getElementById("btn-");
        element.addEventListener("click", self.Minus);

        element = document.getElementById("btn*");
        element.addEventListener("click", self.Multiply);

        element = document.getElementById("btn/");
        element.addEventListener("click", self.Divide);

        element = document.getElementById("btnRemoveLast");
        element.addEventListener("click", self.RemoveLast);

        element = document.getElementById("btnPoint");
        element.addEventListener("click", self.Point);
    }
};